/*
 * Copyrignt (c) qkier.com. All Rights Reserved.
 * Author: zengxiaobao@qkier.com
 * Created Time: 2017-07-05 11:34:28
 * Last Modified: 2017-07-12 11:21:04
 * File Name: washer-profit.go
 * Description:
 */
package statis

import (
	"fmt"

	"qkstatis/db"

	"doubimeizhi.com/datatable"
	"doubimeizhi.com/utility"

	log "github.com/cihub/seelog"
)

var washerProfitStatisInstance *washerProfitStatisService

type washerProfitStatisService struct{}

func GetWasherProfitStatisService() *washerProfitStatisService {
	if washerProfitStatisInstance == nil {
		washerProfitStatisInstance = &washerProfitStatisService{}
	}
	return washerProfitStatisInstance
}

//查询列表
func (p *washerProfitStatisService) ListWasherProfit(ltype int, conditions map[string]interface{}, needTotalRecords bool) ([]*WasherProfit, int) {
	dtq := p.washerProfitDtq(ltype)
	//dtq.SetPage(start, length)
	for key, val := range conditions {
		dtq.PushCondition(key, val)
	}
	sql, values := dtq.GenerateSQL()
	data := make([]*WasherProfit, 0)
	mysqlHelper := db.GetMySQLHelper()
	if results, err := mysqlHelper.Query(sql, values...); err == nil {
		if len(results) == 0 {
			log.Warnf("查询没有统计数据，conditions:%v", conditions)
			return make([]*WasherProfit, 0, 0), 0
		}
		for _, dt := range results {
			data = append(data, p.washerProfitParseRecords(dt, mysqlHelper))
		}
	}
	recordsTotal := 0
	if needTotalRecords {
		s, v := dtq.GenerateTotalRecordSQL("total_records")
		if results, err := mysqlHelper.Query(s, v...); err == nil {
			if len(results) > 0 {
				recordsTotal = mysqlHelper.GetColInt(results[0], "total_records")
			}
		} else {
			log.Errorf("mysql err sql:%s, values:%v", err.Error(), v)
		}
	}
	return data, recordsTotal
}

func (p *washerProfitStatisService) washerProfitDtq(ltype int) *datatable.DataTableQuery {
	dtq := datatable.NewDataTableQuery()
	dtq.PushField(&datatable.FiledItem{Alias: "date_flag", Field: "date_flag", OrderBy: datatable.DESC})
	dtq.PushField(&datatable.FiledItem{Alias: "washer_id", Field: "washer_id"})
	dtq.PushField(&datatable.FiledItem{Alias: "wash_room_id", Field: "wash_room_id"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_order", Field: "washing_order"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_profit", Field: "washing_profit"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_total", Field: "washing_total"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_peer", Field: "washing_peer"})

	switch ltype {
	case 1:
		dtq.SetTableName("statis_washer_profit_day")
	case 2:
		dtq.SetTableName("statis_washer_profit_week")
	case 3:
		dtq.SetTableName("statis_washer_profit_month")
	default:
		log.Errorf("washerProfitDtq, 未知参数ltype； ltype:%d", ltype)
		dtq.SetTableName("none")
	}
	return dtq
}

func (p *washerProfitStatisService) washerProfitParseRecords(dt map[string]interface{}, mysqlHelper *utility.MySQLHelper) *WasherProfit {
	wp := &WasherProfit{}
	wp.DateFlag = mysqlHelper.GetColString(dt, "date_flag")
	if len(wp.DateFlag) == 6 {
		wp.DateFlag += GetStatisService().date2week(wp.DateFlag)
	}
	wp.WasherId = mysqlHelper.GetColString(dt, "washer_id")
	wp.WashRoomId = mysqlHelper.GetColString(dt, "wash_room_id")
	wp.WashingOrder = mysqlHelper.GetColInt(dt, "washing_order")
	wp.WashingPeer = mysqlHelper.GetColInt(dt, "washing_peer")
	wp.WashingTotal = mysqlHelper.GetColInt(dt, "washing_total")

	wpro := mysqlHelper.GetColInt(dt, "washing_profit")
	wp.WashingProfit = fmt.Sprintf("%.2f", float64(wpro)/float64(100))
	return wp
}
